package com.example.web.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.utils.Result;
import com.example.web.system.entity.SysMenu;
import com.example.web.system.entity.SysMenuParam;
import com.example.web.system.service.impl.SysMenuServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author kimido
 * @since 2024-07-04
 */
@RestController
@RequestMapping("/api/menu")
@Api(tags = "菜单管理")
public class SysMenuController {
    @Autowired
    SysMenuServiceImpl sysMenuService;

    @PostMapping("/add")
    @ApiOperation("添加 菜单")
    public Result addMenu(@RequestBody SysMenu sysMenu){
        sysMenu.setDeleted(1);
        boolean save = sysMenuService.save(sysMenu);
        if(save){
            return Result.success();
        }
        return  Result.error(500,"菜单添加失败");
    }

    @GetMapping("/list")
    @ApiOperation("获取菜单列表")
    public Result getMenuList(){
        List<SysMenu> list = sysMenuService.getList();

        return Result.success(list);
    }
    @GetMapping("/parent")
    @ApiOperation("获取父级菜单数据列表")
    public Result getParent(){
        List<SysMenu> parentList = sysMenuService.getParentList();

        return Result.success(parentList);

    }


    @PostMapping("/update")
    @ApiOperation("更新菜单")
    public Result updateMenu(@RequestBody  SysMenu sysMenu){
        boolean b = sysMenuService.updateById(sysMenu);
        if(b){
            return Result.success();
        }
        return Result.error(500,"更新失败");
    }

    @DeleteMapping("/delete/{menuId}")
    @ApiOperation("删除菜单")
    public Result deleteMenu(@PathVariable("menuId") Integer menuId){
        boolean b = sysMenuService.removeById(menuId);
        if(b){
            return Result.success();
        }
        return Result.error(500,"删除失败");
    }
}
